package net.suncaper.psychological.utils;

import net.suncaper.psychological.common.utils.MyDbUtil;
import net.suncaper.psychological.model.domain.VisitRecord;

import java.sql.Connection;
import java.util.List;

public class DatabaseTest {
    public static void main(String[] args) {
        System.out.println("=== 数据库连接测试开始 ===");
        
        try {
            // 测试数据库连接
            Connection conn = MyDbUtil.getConnection();
            System.out.println("数据库连接成功: " + conn);
            
            // 测试简单查询
            String testSql = "SELECT COUNT(*) as count FROM visit_record";
            Object count = MyDbUtil.executeQueryOneValue(testSql);
            System.out.println("visit_record表记录数: " + count);
            
            // 测试初访记录查询
            String sql = "SELECT vr.*, JSON_UNQUOTE(JSON_EXTRACT(u.extra_info, '$.student_id')) as student_id FROM visit_record vr " +
                         "LEFT JOIN appointment a ON vr.appointment_id = a.id " +
                         "LEFT JOIN user u ON a.student_id = u.id " +
                         "WHERE vr.type = 'INITIAL' ORDER BY vr.visit_time DESC";
            
            System.out.println("执行SQL: " + sql);
            List<VisitRecord> records = MyDbUtil.executeQuery(VisitRecord.class, sql);
            System.out.println("查询到 " + records.size() + " 条初访记录");
            
            for (VisitRecord r : records) {
                System.out.println("记录: id=" + r.getId() + 
                                 ", appointment_id=" + r.getAppointment_id() + 
                                 ", student_id=" + r.getStudent_id());
            }
            
            // 关闭连接
            MyDbUtil.closeConnection();
            System.out.println("数据库连接已关闭");
            
        } catch (Exception e) {
            System.err.println("数据库测试失败: " + e.getMessage());
            e.printStackTrace();
        }
        
        System.out.println("=== 数据库连接测试结束 ===");
    }
} 